package com.lkx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lkx.common.BaseContext;
import com.lkx.common.CustomException;
import com.lkx.common.R;
import com.lkx.pojo.ShoppingCart;
import com.lkx.service.ShoppingCartService;

import com.sun.org.apache.bcel.internal.generic.LUSHR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: com.lkx.controller.ShoppingCartController
 * @author: likaixin
 * @create: 2022年11月26日 18:07
 * @desc:
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * @Date:  2022-11-26
     * @Time:  18:08
     * @Param: [shoppingCart]
     * @return: com.lkx.common.R<com.lkx.pojo.ShoppingCart>
     * @desc: 添加购物车
     **/
    @PostMapping ("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
       return shoppingCartService.add(shoppingCart);
    }
    //查看购物车
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @PostMapping("/sub")
    public R<Object> sub(@RequestBody ShoppingCart shoppingCart){


        return shoppingCartService.sub(shoppingCart);
    }
    //清空购物车
    @DeleteMapping("/clean")
    public R<String> deleteShoppingCart(){
        LambdaUpdateWrapper<ShoppingCart> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return R.success("清空购物车成功");
    }
}
